//
//  HWGameCircleTopCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/31.
//

import UIKit

class HWGameCircleTopCell: UICollectionViewCell {
    var bgImageView:UIImageView!
    var collectionView:UICollectionView!
    let collectionProtocol = HWGameCircleTopCellProtocol()
    var groupModelArr:[HWGameCircleGroupModel]?{
        didSet{
            collectionProtocol.groupModelArr = groupModelArr
            collectionView.reloadData()
        }
    }
    var jumpToGroup:((_:String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCircleTopCell {
    private func initUI() -> Void {
        
        contentView.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "gameCircleTopBg")
        contentView.addSubview(bgImageView)
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.scrollDirection = .horizontal
        flowL.minimumLineSpacing = 0
        flowL.minimumInteritemSpacing = 0
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.isPagingEnabled = true
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(HWGameCircleGroupCell.self, forCellWithReuseIdentifier: "HWGameCircleGroupCell")
        contentView.addSubview(collectionView)
        
        collectionProtocol.jumpToGroup = {
            self.jumpToGroup?($0)
        }
        
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(0)
            make.top.equalTo(2*widthScale)
        }
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

class HWGameCircleTopCellProtocol:NSObject,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate, UICollectionViewDataSource{
    
    var groupModelArr:[HWGameCircleGroupModel]?
    
    var jumpToGroup:((_:String)->())?
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.jumpToGroup?(groupModelArr?[indexPath.row].url ?? "")
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return groupModelArr?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize (width: screenWidth/4, height: 146*widthScale)
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let groupCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWGameCircleGroupCell", for: indexPath) as! HWGameCircleGroupCell
        groupCell.model = groupModelArr?[indexPath.row]
        return groupCell
    }
}

class HWGameCircleGroupCell:UICollectionViewCell {
    
    var imageView:UIImageView!
    var nameLabel:UILabel!
    var joinBgView:UIView!
    var joinLabel:UILabel!
    
    var model:HWGameCircleGroupModel?{
        didSet{
            imageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            nameLabel.text = model?.name
            joinLabel.text = model?.title
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCircleGroupCell {
    private func initUI() -> Void {
        imageView = UIImageView()
        imageView.backgroundColor = .clear
        imageView.layer.cornerRadius = 14*widthScale
        imageView.layer.masksToBounds = true
        contentView.addSubview(imageView)
        
        nameLabel = HWLabel.createLabel(title: "", color: "#444444", font: 12*widthScale)
        nameLabel.numberOfLines = 1
        nameLabel.textAlignment = .center
        contentView.addSubview(nameLabel)
        
        joinBgView = UIView()
        joinBgView.frame = CGRect(x: 0, y: 0, width: 66*widthScale, height: 22*widthScale)
        joinBgView.layer.cornerRadius = 4*widthScale
        joinBgView.layer.masksToBounds = true
        contentView.addSubview(joinBgView)
        
        UIColor.gradientColor(view: joinBgView, colors: [
            UIColor.colorWithAlphaHexString(hexString: "#FFB60A", alpha: 1).cgColor,
            UIColor.colorWithAlphaHexString(hexString: "#F66A05", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
        
        joinLabel = HWLabel.createBoldLabel(title: "加入群聊", color: "#FFFFFF", font: 12*widthScale)
        joinBgView.addSubview(joinLabel)
    }
    
    private func initFrame() -> Void {
        imageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(17*widthScale)
            make.width.height.equalTo(56*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(6*widthScale)
            make.left.equalTo(5*widthScale)
            make.right.equalTo(-5*widthScale)
            make.centerX.equalToSuperview()
        }
        
        joinBgView.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom).offset(6*widthScale)
            make.width.equalTo(66*widthScale)
            make.height.equalTo(22*widthScale)
            make.centerX.equalToSuperview()
        }
        
        joinLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
}
